<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .upload{
            height: 100px;
            line-height: 100px;
            text-align: center;
            width: 100px;
            border: 1px solid #ddd;
            position: relative;
        }
        .plus{
            font-size:28px;
        }
        input{
            top:0;
            left:0;
            position: absolute;
            width: 100px;
            height: 100px;
            opacity: 0;
        }

    </style>
</head>
<body>
    <div class="upload">
        <span class="plus">+</span>
        <input type="file" name="" id="input" accept=".jpg, .jpeg, .png">
    </div>
    <img src="" id="img" alt="">
</body>
<!-- https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input/file -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    document.getElementById("input").addEventListener("change",function(e){
        console.log(e.target.files);// 伪数组
        Array.from(e.target.files).map((f=>{
            // 构造form表单格式数据
            let formData = new FormData();
            formData.append("file",f);
            // http://linweiqin.cn:8001/v1/addimg/avatar post form表单格式数据
            axios.post("http://linweiqin.cn:8001/v1/addimg/avatar",formData).then(res=>{
                console.log(res);
                // 拼接图片在线地址
                let imageUrl = "http://linweiqin.cn:8001/img/"+res.data.image_path;
                document.getElementById("img").setAttribute("src",imageUrl);
            })
           
        }))
    })
</script>

</html>